package com.loongtech.bi.action.biplayer;

import com.loongtech.bi.action.BaseBiSearchAction;
import com.loongtech.bi.manager.player.BackFuncManager;
import com.loongtech.bi.manager.player.BackFuncManager.BackDevicePlatformData;
import com.loongtech.bi.manager.player.BackFuncManager.BackUserPlatformData;
import com.loongtech.bi.support.chart.ChartTool;
import com.loongtech.bi.support.chart.ChartType;
import com.loongtech.bi.support.chart.ChartUIData;
import com.loongtech.core.util.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 玩家分析-回流分析Action
 * 
 * @author xckuang
 * @date 2015年11月21日
 */
@Controller
@Scope("session")
@RequestMapping("/player/backPlayer")
public class BackPlayerBiAction extends BaseBiSearchAction {
	private static final long serialVersionUID = 1L;

	private ChartUIData backUser07DaysChart1;   // 账号07日回流图柱状
	private ChartUIData backUser14DaysChart1;   // 账号14日回流图柱状
	private ChartUIData backUser30DaysChart1;   // 账号30日回流图柱状
	private ChartUIData backDevice07DaysChart1; // 设备07日回流图柱状
	private ChartUIData backDevice14DaysChart1; // 设备14日回流图柱状
	private ChartUIData backDevice30DaysChart1; // 设备30日回流图柱状

	private ChartUIData backUser07DaysChart2;   // 账号07日回流图折线
	private ChartUIData backUser14DaysChart2;   // 账号14日回流图折线
	private ChartUIData backUser30DaysChart2;   // 账号30日回流图折线
	private ChartUIData backDevice07DaysChart2; // 设备07日回流图折线
	private ChartUIData backDevice14DaysChart2; // 设备14日回流图折线
	private ChartUIData backDevice30DaysChart2; // 设备30日回流图折线

	private List<BackUserPlatformData> backUserDatas;     // 账号回流数据
	private List<BackDevicePlatformData> backDeviceDatas; // 设备回流数据

	@Autowired
	private BackFuncManager backManager;

	@RequestMapping(value = "initParam.do", method = { RequestMethod.GET, RequestMethod.POST })
	public void initParam(HttpServletRequest request, HttpServletResponse response, String callback) {
		try {
			Map<String, Object> result = new HashMap<>();
			result.put("res", "0");
			result.put("serverItmes", serverItmes);
            result.put("channelItmes", channelItmes);
            result.put("platformItmes", platformItmes);
			printToJson(response, result, callback);
		} catch (Exception e) {
			e.printStackTrace();
			Map<String, Object> result = new HashMap<>();
			result.put("res", "1");
			result.put("msg", e.getMessage());
			printToJson(response, result, callback);
			return;
		}
	}

	@RequestMapping(value = "search.do", method = { RequestMethod.GET, RequestMethod.POST })
	public void search(HttpServletRequest request, HttpServletResponse response, @RequestParam String dstart,
			@RequestParam String dend, @RequestParam String platform, @RequestParam String channel, @RequestParam String server, String callback) {
		Map<String, Object> result = new HashMap<>();
		try {
			dateStart = Time.formatDate(dstart, Time.TIMEPATTERN_SIMPLE);
			dateEnd   = Time.formatDate(dend, Time.TIMEPATTERN_SIMPLE);
			queryPlatform = platform;
			queryChannel = channel;
			queryServer = server;

			backUserDatas = backManager.getBackUserData(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
			backDeviceDatas = backManager.getBackDeviceData(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);

			result.put("res", "0");
			result.put("platform", platform);
			result.put("channel", channel);
			result.put("server", server);
			// 表格数据
			result.put("backUserDatas", backUserDatas);
			result.put("backDeviceDatas", backDeviceDatas);

			Class<?> clazz1 = BackFuncManager.BackUserPlatformData.class;
			Class<?> clazz2 = BackFuncManager.BackDevicePlatformData.class;
			
			// 构造柱形图
			backUser07DaysChart1 = new ChartUIData();
			backUser07DaysChart1.setJsonType(ChartType.COLUMN.type);
			backUser07DaysChart1.setJsonStr(new ChartTool(backUserDatas, clazz1, "backuser7day").loadData(backUser07DaysChart1.getJsonType()));
			
			backUser14DaysChart1 = new ChartUIData();
			backUser14DaysChart1.setJsonType(ChartType.COLUMN.type);
			backUser14DaysChart1.setJsonStr(new ChartTool(backUserDatas, clazz1, "backuser14day").loadData(backUser14DaysChart1.getJsonType()));
			
			backUser30DaysChart1 = new ChartUIData();
			backUser30DaysChart1.setJsonType(ChartType.COLUMN.type);
			backUser30DaysChart1.setJsonStr(new ChartTool(backUserDatas, clazz1, "backuser30day").loadData(backUser30DaysChart1.getJsonType()));
			
			backDevice07DaysChart1 = new ChartUIData();
			backDevice07DaysChart1.setJsonType(ChartType.COLUMN.type);
			backDevice07DaysChart1.setJsonStr(new ChartTool(backDeviceDatas, clazz2, "macOldUser7day").loadData(backDevice07DaysChart1.getJsonType()));
			
			backDevice14DaysChart1 = new ChartUIData();
			backDevice14DaysChart1.setJsonType(ChartType.COLUMN.type);
			backDevice14DaysChart1.setJsonStr(new ChartTool(backDeviceDatas, clazz2, "macOldUser14day").loadData(backDevice14DaysChart1.getJsonType()));
			
			backDevice30DaysChart1 = new ChartUIData();
			backDevice30DaysChart1.setJsonType(ChartType.COLUMN.type);
			backDevice30DaysChart1.setJsonStr(new ChartTool(backDeviceDatas, clazz2, "macOldUser30day").loadData(backDevice30DaysChart1.getJsonType()));

			// 构造折线图
			backUser07DaysChart2 = new ChartUIData();
			backUser07DaysChart2.setJsonType(ChartType.SPLINE.type);
			backUser07DaysChart2.setJsonStr(new ChartTool(backUserDatas, clazz1, "backrate7day").loadData(backUser07DaysChart2.getJsonType()));
			
			backUser14DaysChart2 = new ChartUIData();
			backUser14DaysChart2.setJsonType(ChartType.SPLINE.type);
			backUser14DaysChart2.setJsonStr(new ChartTool(backUserDatas, clazz1, "backrate14day").loadData(backUser14DaysChart2.getJsonType()));
			
			backUser30DaysChart2 = new ChartUIData();
			backUser30DaysChart2.setJsonType(ChartType.SPLINE.type);
			backUser30DaysChart2.setJsonStr(new ChartTool(backUserDatas, clazz1, "backrate30day").loadData(backUser30DaysChart2.getJsonType()));
			
			backDevice07DaysChart2 = new ChartUIData();
			backDevice07DaysChart2.setJsonType(ChartType.SPLINE.type);
			backDevice07DaysChart2.setJsonStr(new ChartTool(backDeviceDatas, clazz2, "oldUserMacRate7day").loadData(backDevice07DaysChart2.getJsonType()));
			
			backDevice14DaysChart2 = new ChartUIData();
			backDevice14DaysChart2.setJsonType(ChartType.SPLINE.type);
			backDevice14DaysChart2.setJsonStr(new ChartTool(backDeviceDatas, clazz2, "oldUserMacRate14day").loadData(backDevice14DaysChart2.getJsonType()));
			
			backDevice30DaysChart2 = new ChartUIData();
			backDevice30DaysChart2.setJsonType(ChartType.SPLINE.type);
			backDevice30DaysChart2.setJsonStr(new ChartTool(backDeviceDatas, clazz2, "oldUserMacRate30day").loadData(backDevice30DaysChart2.getJsonType()));

			// 柱状图6个
			result.put("backUser07DaysChart1", backUser07DaysChart1);
			result.put("backUser14DaysChart1", backUser14DaysChart1);
			result.put("backUser30DaysChart1", backUser30DaysChart1);
			result.put("backDevice07DaysChart1", backDevice07DaysChart1);
			result.put("backDevice14DaysChart1", backDevice14DaysChart1);
			result.put("backDevice30DaysChart1", backDevice30DaysChart1);

			// 折线图6个
			result.put("backUser07DaysChart2", backUser07DaysChart2);
			result.put("backUser14DaysChart2", backUser14DaysChart2);
			result.put("backUser30DaysChart2", backUser30DaysChart2);
			result.put("backDevice07DaysChart2", backDevice07DaysChart2);
			result.put("backDevice14DaysChart2", backDevice14DaysChart2);
			result.put("backDevice30DaysChart2", backDevice30DaysChart2);

			printToJson(response, result, callback);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("res", "1");
			result.put("platform", platform);
			result.put("msg", e.getMessage());
			printToJson(response, result, callback);
		}
	}

	public void search() {
	}

	@Override
	protected String csvContent() {
		return null;
	}

	@Override
	protected String csvFileName() {
		return "回流分析";
	}

	public ChartUIData getBackUser07DaysChart1() {
		return backUser07DaysChart1;
	}

	public void setBackUser07DaysChart1(ChartUIData backUser07DaysChart1) {
		this.backUser07DaysChart1 = backUser07DaysChart1;
	}

	public ChartUIData getBackUser14DaysChart1() {
		return backUser14DaysChart1;
	}

	public void setBackUser14DaysChart1(ChartUIData backUser14DaysChart1) {
		this.backUser14DaysChart1 = backUser14DaysChart1;
	}

	public ChartUIData getBackUser30DaysChart1() {
		return backUser30DaysChart1;
	}

	public void setBackUser30DaysChart1(ChartUIData backUser30DaysChart1) {
		this.backUser30DaysChart1 = backUser30DaysChart1;
	}

	public ChartUIData getBackDevice07DaysChart1() {
		return backDevice07DaysChart1;
	}

	public void setBackDevice07DaysChart1(ChartUIData backDevice07DaysChart1) {
		this.backDevice07DaysChart1 = backDevice07DaysChart1;
	}

	public ChartUIData getBackDevice14DaysChart1() {
		return backDevice14DaysChart1;
	}

	public void setBackDevice14DaysChart1(ChartUIData backDevice14DaysChart1) {
		this.backDevice14DaysChart1 = backDevice14DaysChart1;
	}

	public ChartUIData getBackDevice30DaysChart1() {
		return backDevice30DaysChart1;
	}

	public void setBackDevice30DaysChart1(ChartUIData backDevice30DaysChart1) {
		this.backDevice30DaysChart1 = backDevice30DaysChart1;
	}

	public ChartUIData getBackUser07DaysChart2() {
		return backUser07DaysChart2;
	}

	public void setBackUser07DaysChart2(ChartUIData backUser07DaysChart2) {
		this.backUser07DaysChart2 = backUser07DaysChart2;
	}

	public ChartUIData getBackUser14DaysChart2() {
		return backUser14DaysChart2;
	}

	public void setBackUser14DaysChart2(ChartUIData backUser14DaysChart2) {
		this.backUser14DaysChart2 = backUser14DaysChart2;
	}

	public ChartUIData getBackUser30DaysChart2() {
		return backUser30DaysChart2;
	}

	public void setBackUser30DaysChart2(ChartUIData backUser30DaysChart2) {
		this.backUser30DaysChart2 = backUser30DaysChart2;
	}

	public ChartUIData getBackDevice07DaysChart2() {
		return backDevice07DaysChart2;
	}

	public void setBackDevice07DaysChart2(ChartUIData backDevice07DaysChart2) {
		this.backDevice07DaysChart2 = backDevice07DaysChart2;
	}

	public ChartUIData getBackDevice14DaysChart2() {
		return backDevice14DaysChart2;
	}

	public void setBackDevice14DaysChart2(ChartUIData backDevice14DaysChart2) {
		this.backDevice14DaysChart2 = backDevice14DaysChart2;
	}

	public ChartUIData getBackDevice30DaysChart2() {
		return backDevice30DaysChart2;
	}

	public void setBackDevice30DaysChart2(ChartUIData backDevice30DaysChart2) {
		this.backDevice30DaysChart2 = backDevice30DaysChart2;
	}

	public BackFuncManager getBackManager() {
		return backManager;
	}

	public void setBackManager(BackFuncManager backManager) {
		this.backManager = backManager;
	}

	public List<BackUserPlatformData> getBackUserDatas() {
		return backUserDatas;
	}

	public void setBackUserDatas(List<BackUserPlatformData> backUserDatas) {
		this.backUserDatas = backUserDatas;
	}

	public List<BackDevicePlatformData> getBackDeviceDatas() {
		return backDeviceDatas;
	}

	public void setBackDeviceDatas(List<BackDevicePlatformData> backDeviceDatas) {
		this.backDeviceDatas = backDeviceDatas;
	}
}
